package com.zhangmeng.tools.components;

import javafx.beans.property.SimpleIntegerProperty;
import javafx.beans.value.ChangeListener;
import javafx.beans.value.ObservableValue;
import javafx.embed.swing.SwingNode;
import javafx.geometry.Bounds;
import javafx.scene.layout.AnchorPane;
import org.fife.ui.rsyntaxtextarea.RSyntaxTextArea;
import org.fife.ui.rsyntaxtextarea.RSyntaxTextAreaUI;
import org.fife.ui.rsyntaxtextarea.SyntaxConstants;
import org.fife.ui.rsyntaxtextarea.Theme;
import org.fife.ui.rtextarea.RTextScrollPane;

import javax.swing.*;
import javax.swing.event.CaretEvent;
import javax.swing.event.CaretListener;
import java.awt.*;
import java.awt.event.ContainerEvent;
import java.awt.event.ContainerListener;
import java.io.IOException;
import java.lang.reflect.InvocationTargetException;

public class RxEditer extends AnchorPane{

    private final SimpleIntegerProperty line = new SimpleIntegerProperty();
    private final SimpleIntegerProperty column = new SimpleIntegerProperty();

    private final  RSyntaxTextArea textArea = new RSyntaxTextArea();

    private RTextScrollPane sp;

    public RSyntaxTextArea getTextArea(){
        return textArea;
    }

    public void setText(String text){
        textArea.setText(text);
    }

    public void setFont(Font font){
        textArea.setFont(font);
    }

    //选择全部
    public void selectAll(){
        textArea.selectAll();
    }

    private void changeStyleViaThemeXml(String them_name) {
        try {
            Theme theme = Theme.load(getClass().getResourceAsStream(
                    "/org/fife/ui/rsyntaxtextarea/themes/" + them_name + ".xml"));
            theme.apply(textArea);
        } catch (IOException ioe) { // Never happens
            ioe.printStackTrace();
        }
    }

    public void setTheme(String them_name){
        changeStyleViaThemeXml(them_name);
    }

    public RxEditer(String style,String theme) {
        SwingNode sn = new SwingNode();
        new Thread(()->{
            SwingUtilities.invokeLater(() -> {

                textArea.setEditable(true);//编辑器
                if (style == null || style.equals("")) {
                    textArea.setSyntaxEditingStyle(SyntaxConstants.SYNTAX_STYLE_JAVA);//java
                }else {
                    textArea.setSyntaxEditingStyle(style);
                }
                textArea.setCodeFoldingEnabled(true);//折叠代码
                textArea.addCaretListener(e -> {
                    JTextArea editArea = (JTextArea) e.getSource();
                    int linenum = 1;
                    int columnnum = 1;
                    try {
                        int caretpos = editArea.getCaretPosition();
                        linenum = editArea.getLineOfOffset(caretpos);
                        columnnum = caretpos - editArea.getLineStartOffset(linenum);
                        linenum += 1;
                        columnnum += 1;
                    } catch (Exception ex) {
                        ex.printStackTrace();
                    }
                    line.setValue(linenum);
                    column.setValue(columnnum);
                });
                sp= new RTextScrollPane(textArea);
                changeStyleViaThemeXml(theme);
                sp.setBorder(null);
                sn.setContent(sp);
            });
        }).start();

        widthProperty().addListener(new ChangeListener<Number>() {
            @Override
            public void changed(ObservableValue<? extends Number> observable, Number oldValue, Number newValue) {
                textArea.setSize(newValue.intValue(),textArea.getHeight());
            }
        });

        heightProperty().addListener(new ChangeListener<Number>() {
            @Override
            public void changed(ObservableValue<? extends Number> observable, Number oldValue, Number newValue) {
                textArea.setSize(textArea.getWidth(),newValue.intValue());
            }
        });

        getChildren().add(sn);
        setTopAnchor(sn, 0.0);
        setBottomAnchor(sn, 0.0);
        setLeftAnchor(sn, 0.0);
        setRightAnchor(sn, 0.0);
    }
}
